package com.lnt.mq.rabbitmq.pubsub;

import com.lnt.mq.rabbitmq.util.RabbitConstant;
import com.lnt.mq.rabbitmq.util.RabbitUtil;
import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * 模拟客户端
 * 本次测试接收点的客户应用端
 * 扮演信息接受者行为
 */
public class Sina {

    public static void main(String[] args) throws IOException {
        Connection connection = RabbitUtil.getConnection();

        final Channel channel = connection.createChannel();

        // 声明队列信息
        channel.queueDeclare(RabbitConstant.QUEUE_SINA, false, false, false, null);

        // 队列绑定交换机
        // param1: 队列名
        // param2: 需要绑定的交换机名称
        // param3: 路由KEY
        channel.queueBind(RabbitConstant.QUEUE_SINA, RabbitConstant.EXCHANGE_WEATHER, "");
        channel.basicQos(1);

        channel.basicConsume(RabbitConstant.QUEUE_SINA, false, new DefaultConsumer(channel){

            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("新浪天气收到气象信息:" + new String(body));
                channel.basicAck(envelope.getDeliveryTag(), false);
            }
        });
    }
}
